﻿using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// OperationDetailsCtl.xaml 的交互逻辑
    /// </summary>
    public partial class OperationDetailsItem : UserControl
    {
        public OperationDetailsItem()
        {
            InitializeComponent();
            this._noTypeAggregateModelList.LoadingRow += NoTypeAggregateModelList_LoadingRow;

        }
        private void NoTypeAggregateModelList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            string tag = checkBox.Tag.ToString();
            if (string.IsNullOrEmpty(tag))
            {
                return;
            }
            else if (tag.Equals("isEnd"))
            {
                OperationDetailsViewModel.Instance.UpdateEndTime(checkBox.DataContext as NoTypeAggregateModel, (bool)checkBox.IsChecked);
            }
            else if (tag.Equals("takeOut"))
            {
                if ((checkBox.DataContext as NoTypeAggregateModel).PatientDrugsModel.EndTime == new DateTime())
                {
                    MessageBoxCommon.ShowDialog("液体未结束，不能带走!", MessageBoxCommon.OK);
                    return;
                }
                OperationDetailsViewModel.Instance.ChangeTakeOut(checkBox.DataContext as NoTypeAggregateModel, (bool)checkBox.IsChecked);
            }

        }

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NoTypeAggregateModel noTypeAggregateModel = ((sender as TextBlock).Tag as NoTypeAggregateModel);
            if (noTypeAggregateModel.PatientDrugsModel != null)
            {
                if (Constants.DRUG_SPECIAL.Equals(noTypeAggregateModel.PatientDrugsModel.Type))
                {
                    SingleDrugConfigCtrlViewModel.Instance.PatientDrugsModel = noTypeAggregateModel.PatientDrugsModel;
                    SingleDrugConfigCtrlViewModel.Instance.SetPatientDrug();
                    DrugAndEventWindow.Instance.CusContent = SingleDrugConfigDialogCtrl.Instance;
                    DrugAndEventWindow.Instance.Title = "编辑单次用药";
                    DrugAndEventWindow.Instance.ShowDialog();
                }
                else
                {
                    if (Constants.DRUG_SHUYE.Equals(noTypeAggregateModel.PatientDrugsModel.Type))
                    {
                        MedicineConfigCtrlViewModel.Instance.DialogType = 2;
                    }
                    else if (Constants.DRUG_SHUZHONG.Equals(noTypeAggregateModel.PatientDrugsModel.Type))
                    {
                        MedicineConfigCtrlViewModel.Instance.DialogType = 1;
                    }
                    MedicineConfigCtrlViewModel.Instance.PatientDrugsModel = noTypeAggregateModel.PatientDrugsModel;
                    MedicineConfigCtrlViewModel.Instance.SetPatientDrug();
                    DrugAndEventWindow.Instance.CusContent = MedicineConfigDialogCtrl.Instance;
                    DrugAndEventWindow.Instance.Title = "编辑用药输液";
                    DrugAndEventWindow.Instance.ShowDialog();
                }
            }
            if (noTypeAggregateModel.PatientEventsModel != null)
            {
                ConfigEventsModel cem = null;
                if (DataManager.Instance.Code2events.ContainsKey(noTypeAggregateModel.PatientEventsModel.Code))
                {
                    cem = DataManager.Instance.Code2events[noTypeAggregateModel.PatientEventsModel.Code];
                }
                if (cem == null)
                {
                    return;
                }
                else
                {

                    if (cem.Single)
                    {
                        EventAndDrug eventAndDrug = new EventAndDrug();
                        eventAndDrug.IsDrug = false;
                        eventAndDrug.IsEnd = false;
                        eventAndDrug.PatientEvents = noTypeAggregateModel.PatientEventsModel;
                        eventAndDrug.Time = noTypeAggregateModel.PatientEventsModel.BeginTime;
                        eventAndDrug.Name = cem.Name;
                        EventAndDrugDialogCtrl.Instance.IsAutoShow = true;
                        EventAndDrugDialogCtrl.Instance.EventAndDrug = eventAndDrug;
                        DrugAndEventWindow.Instance.CusContent = EventAndDrugDialogCtrl.Instance;
                        DrugAndEventWindow.Instance.Title = "编辑事件";
                        DrugAndEventWindow.Instance.ShowDialog();
                    }
                    else
                    {
                        ContinueEventEditCtrl continueEventEditCtrl = new ContinueEventEditCtrl();
                        continueEventEditCtrl.PatientEvent = noTypeAggregateModel.PatientEventsModel;
                        DrugAndEventWindow.Instance.CusContent = continueEventEditCtrl;
                        DrugAndEventWindow.Instance.Title = "编辑事件";
                        DrugAndEventWindow.Instance.ShowDialog();
                    }
                }
            }
        }
    }
}
